package cn.mjnxin.iot.base.util;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

/**
 * 读取配置工具
 * @author CLG 2023年10月10日
 * @version V1.0.0
 */
public class ConfigTool {
    public static Map<String, Object> readConfigParams(String path) throws Exception {
        Map<String, Object> properties = new HashMap<>();
        RandomAccessFile raf = null;
//        int line = 0;
        try {
        	System.out.println("扫描 " + path + " 读取配置信息");
            raf = new RandomAccessFile(new File(path), "r");
            String lineStr = null;
            while ((lineStr = raf.readLine()) != null) {
//            	line++;
            	lineStr = new String(lineStr.getBytes("ISO-8859-1"),"utf-8");
                lineStr = lineStr.trim();
                // 忽略空行
                if (checkLine(lineStr) == null) {
//                	System.out.println("略过第 " + line + " 行");
                    continue;
                }

                if (lineStr.startsWith("[") && lineStr.endsWith("]")) {
                    String key = lineStr.substring(1, lineStr.length() - 1);
                    HashMap<String, String> valueMap = new HashMap<>();
                    properties.put(key, valueMap);
                    long pointer;
                    while (((pointer = raf.getFilePointer()) > 0)
                            && ((lineStr = raf.readLine()) != null)) {
//                    	line++;
                        lineStr = lineStr.trim();
                        // 忽略空行
                        if (checkLine(lineStr) == null) {
//                        	System.out.println("略过第 " + line + " 行");
                            continue;
                        }

                        if (lineStr.startsWith("[")) {
                            // 返回上一行退出循环
                            raf.seek(pointer);
//                            line--;
                            break;
                        }

                        if (lineStr.indexOf("=") < 0) {
//                        	System.out.println("略过第 " + line + " 行");
                            continue;
                        }

                        String[] kv = lineStr.split("=", -1);
                        valueMap.put(kv[0].trim(), new String(kv[1].trim().getBytes("ISO-8859-1"), "GBK"));
                    }
                }
                else{
                	//自由变量的读取
                    String[] kv = lineStr.split("=", -1);
                    properties.put(kv[0].trim(), new String(kv[1].trim().getBytes("ISO-8859-1"), "GBK"));
                }
            }
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return properties;
    }

    private static String checkLine(String lineStr) {
        lineStr = lineStr.trim().replaceAll("\\p{C}", "");
        // 忽略空行
        if (lineStr.isEmpty()) {
            return null;
        }
        // 忽略注释
        if (lineStr.startsWith("#") || lineStr.startsWith("//")) {
            return null;
        }

        return lineStr;
    }
}
